package ru.ifmo.ipimenov.ewf_svn.standalone.view.gui;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import ru.ifmo.ipimenov.ewf_svn.standalone.controller.LoginAction;

import javax.swing.*;
import java.awt.*;

/**
 * @author: Ilya Pimenov
 * Scartel Starlab company
 */
public class LoginForm extends JFrame {
    /**
     * Logger for all purposes
     */
    private static Log log = LogFactory.getLog(LoginForm.class);

    private JButton loginButton;
    private JTextField username;
    private JPasswordField password;

    private JList list;

    private Container contentPane;

    public LoginForm() {
        super("Authenticate");

        setPreferredSize(new Dimension(240, 120));
        int yLocation = (int)(Toolkit.getDefaultToolkit().getScreenSize().getHeight() / 2 - getPreferredSize().getHeight() / 2);
        int xLocation = (int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2 - getPreferredSize().getWidth() / 2);
        setLocation(xLocation, yLocation);

        username = new JTextField("ilya.pimenov");
        password = new JPasswordField("n7s9m9d3");
        
        loginButton = new JButton("Login");
        loginButton.addActionListener(new LoginAction(username, password, this));

        contentPane = getContentPane();
        addComponentsToPane();
    }

    private void addComponentsToPane() {
        JPanel p = new JPanel();

        p.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 0.2;
        gbc.gridx = 1;
        gbc.gridy = 0;
        p.add(new JLabel("Username: "), gbc);
        
        gbc.weightx = 0.7;
        gbc.gridx = 3;
        gbc.gridy = 0;
        p.add(username, gbc);

        gbc.weightx = 0.2;
        gbc.gridx = 1;
        gbc.gridy = 1;
        p.add(new JLabel("password: "), gbc);

        gbc.weightx = 0.7;
        gbc.gridx = 3;
        gbc.gridy = 1;
        p.add(password, gbc);

        contentPane.setLayout(new BorderLayout(13, 13));

        if (!(contentPane.getLayout() instanceof BorderLayout)) {
            contentPane.add(new JLabel("Container doesn't use BorderLayout."));
            return;
        }

        contentPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

        contentPane.add(p, BorderLayout.CENTER);

        contentPane.add(loginButton, BorderLayout.PAGE_END);
    }
}